package com.ycrlkj.android.modulescore.main.activity.app;

import android.content.Context;
import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.DeleteObjectRequest;
import com.alibaba.sdk.android.oss.model.DeleteObjectResult;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.blankj.utilcode.util.ToastUtils;
import com.ycrlkj.android.modulescore.main.entity.AliSts;

public class AliYunAndroidUtil {
    private AliYunUploadView aliyunUploadView;
    private OSSCredentialProvider credentialProvider;
    private ClientConfiguration conf;
    private OSS oss;

    public AliYunAndroidUtil(AliYunUploadView aliyunUploadView) {
        this.aliyunUploadView = aliyunUploadView;
    }

    public void UploadFile(Context context, AliSts aliSts, String fileName, String uploadPath) {

        credentialProvider = new OSSStsTokenCredentialProvider(aliSts.getAccessKeyId(), aliSts.getAccessKeySecret(), aliSts.getSecurityToken());
        conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求数，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次

        oss = new OSSClient(context, aliSts.getEndpoint(), credentialProvider, conf);

        String bucketName = aliSts.getBucketName();

        // 构造上传请求。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, uploadPath);

        // 异步上传时可以设置进度回调。
        putObjectRequest.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {

            }
        });

        OSSAsyncTask ossAsyncTask = oss.asyncPutObject(putObjectRequest, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                aliyunUploadView.UploadSuccess(oss.presignPublicObjectURL(bucketName, fileName));
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                if (clientExcepion != null) {
                    clientExcepion.printStackTrace();
                    ToastUtils.showLong("上传失败");
                    aliyunUploadView.UploadFail("上传失败");
                }

                if (serviceException != null) {
                    ToastUtils.showLong("网络错误, 上传失败");
                    aliyunUploadView.UploadFail("网络错误, 上传失败");
                }
            }
        });

        // ossAsyncTask.cancel(); // 取消任务
        // ossAsyncTask.waitUntilFinished(); // 等待任务完成
    }

    public void DeleteFile(Context context, AliSts aliSts, String fileName, String uploadPath) {

        credentialProvider = new OSSStsTokenCredentialProvider(aliSts.getAccessKeyId(), aliSts.getAccessKeySecret(), aliSts.getSecurityToken());
        conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求数，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次

        oss = new OSSClient(context, aliSts.getEndpoint(), credentialProvider, conf);

        // 创建删除请求。
        DeleteObjectRequest delete = new DeleteObjectRequest(aliSts.getBucketName(), fileName);
        // 异步删除。
        OSSAsyncTask deleteTask = oss.asyncDeleteObject(delete, new OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult>() {
            @Override
            public void onSuccess(DeleteObjectRequest request, DeleteObjectResult result) {

            }

            @Override
            public void onFailure(DeleteObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                if (clientExcepion != null) {
                    clientExcepion.printStackTrace();
                    ToastUtils.showLong("上传失败");
                    aliyunUploadView.UploadFail("上传失败");
                }

                if (serviceException != null) {
                    ToastUtils.showLong("网络错误, 上传失败");
                    aliyunUploadView.UploadFail("网络错误, 上传失败");
                }
            }
        });

        // deleteTask.cancel(); // 可以取消任务
        // deleteTask.waitUntilFinished(); // 等待任务完成
    }

    public interface AliYunUploadView {
        void UploadSuccess(String url);

        void UploadFail(String error);
    }
}
